package com.yxkj.railservicer.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.yxkj.railservicer.R;

import java.io.File;
import java.text.DecimalFormat;


/**
 * @author neil
 * @2016年8月19日上午11:20:26
 * @TODO app小工具：常见单个方法，零散的，app工程(建议放到这里)
 */
public class AppUtils extends Utils {
    /****
     * 隐藏软件盘
     *
     * @param mContext
     */
    public static void hideSoft(Context mContext) {
        ((Activity) mContext).getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    /**
     * @TODO dp转px
     */
    public static int dp2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * @TODO px转dp
     */
    public static int px2dp(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /***
     * 格式化数据
     *
     * @param num
     * @return
     */
    public static String formatDobleNum(double num) {
        return new DecimalFormat("#####0.00").format(num);
    }

    /**
     * 打开各种类型的文件
     *
     * @param
     */
    public static void startFileMimeType(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file), MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase()));
        context.startActivity(intent);
    }

    /***
     * 确定 ，取消 的dialog
     *
     * @param activity
     * @param callBack
     */
    public static void showMsgDialog(Context activity, String message, String confirm, String cancle, int type, String time, final MsgCallBack callBack) {
        final MessageDialog d = new MessageDialog(activity, message, type);
        d.setPositiveButton(confirm, new BaseDialog.OnDialogButtonClickListener() {
            @Override
            public void onClick(Dialog dialog, View v) {
                callBack.confirmMsg(d);
            }
        });
        d.setNegativeButton(cancle, new BaseDialog.OnDialogButtonClickListener() {
            @Override
            public void onClick(Dialog dialog, View v) {
                callBack.cancelMsg(d);
            }
        });

        if (null != time) {
            d.setTime(time);

        } else {

        }

        try {
            d.showDialog();
        } catch (Exception e) {
        }
    }

    /***
     * 返回数据的的dialog
     *
     * @param activity
     * @param callBack
     */
    public static void showMsgDialog(final Context activity, String message, final String data1, final String data2, int type, final MsgCallBackData callBack) {
        final MessageDialog d = new MessageDialog(activity, message, type);
        d.setPositiveButton(data1, new BaseDialog.OnDialogButtonClickListener() {
            @Override
            public void onClick(Dialog dialog, View v) {
                callBack.clickMsg(d, data1);
            }
        });
        d.setNegativeButton(data2, new BaseDialog.OnDialogButtonClickListener() {
            @Override
            public void onClick(Dialog dialog, View v) {
                callBack.clickMsg(d, data2);
            }
        });
        try {
            d.showDialog();
        } catch (Exception e) {
        }
    }

    /***
     * 返回loading的的dialog
     */
    public static Dialog getLoadingDialog(Context activity, String title, String content) {
        Dialog dialog = new Dialog(activity, R.style.MyDialog);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_message_loading, null);
        TextView tv1 = (TextView) view.findViewById(R.id.dialog_message);
        TextView tv2 = (TextView) view.findViewById(R.id.dialog_content);
        tv1.setText(title);
        tv2.setText(content);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.color.pub_color_transparent);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.alpha = 1.0f;
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);

        WindowManager windowManager = ((Activity) activity).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()) * 3 / 4;
        dialog.getWindow().setAttributes(lp);
//        ProgressDialog dialog = new ProgressDialog(activity, R.style.trans_dialog);
        return dialog;
    }

    public static void showMsgDialog(Context activity, int i, final MsgCallBackData2 callBack) {
        final Dialog dialog = new Dialog(activity, R.style.MyDialog);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_message_validate, null);

        final EditText et_validate = (EditText) view.findViewById(R.id.et_validate);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        final TextView tv_error = (TextView) view.findViewById(R.id.tv_error);

        dialog.setContentView(view);


        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = et_validate.getText().toString();
                callBack.clickMsg(dialog, text, tv_error);
//                dialog.dismiss();
            }
        });

        et_validate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tv_error.setVisibility(View.INVISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        try {
            dialog.show();
        } catch (Exception e) {

        }

    }

    /***
     * 手动输入原因的的dialog
     *
     * @param activity
     * @param callBack
     */
    public static void showMsgDialog(final Context activity, final MsgCallBackData callBack) {
        final Dialog dialog = new Dialog(activity, R.style.MyDialog);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_message_refuse_2, null);
        dialog.setContentView(view);
        TextView dialog_message = (TextView) view.findViewById(R.id.dialog_message);
        final RadioButton btn1 = (RadioButton) view.findViewById(R.id.rb_rea1);
        final RadioButton btn2 = (RadioButton) view.findViewById(R.id.rb_rea2);
        final RadioButton btn3 = (RadioButton) view.findViewById(R.id.rb_rea3);
        final EditText et_input = (EditText) view.findViewById(R.id.et_reason);
        et_input.setEnabled(false);
        et_input.setBackgroundResource(R.drawable.shape_gray_stroke);
        Button confirm = (Button) view.findViewById(R.id.confirm);
        dialog_message.setText("拒绝退单原因");
        confirm.setText("确认提交");
        ImageView iv_cancle = (ImageView) view.findViewById(R.id.iv_cancle);
        iv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_input.setEnabled(false);
                et_input.setBackgroundResource(R.drawable.shape_gray_stroke);
                et_input.setText("");
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_input.setEnabled(false);
                et_input.setBackgroundResource(R.drawable.shape_gray_stroke);
                et_input.setText("");
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_input.setEnabled(true);
                et_input.setBackgroundResource(R.drawable.shape_orange_stroke);
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = null;
                if (btn1.isChecked()) {
                    text = btn1.getText().toString();
                } else if (btn2.isChecked()) {
                    text = btn2.getText().toString();
                } else if (btn3.isChecked()) {
                    text = et_input.getText().toString();
                    if (TextUtils.isEmpty(text)) {
                        Toast.makeText(activity, "请输入拒绝原因", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } else {
//                    ToastUtils.showMsg(activity, "请选择或输入拒绝原因");
                    Toast.makeText(activity, "请选择或输入拒绝原因", Toast.LENGTH_SHORT).show();
                }
                callBack.clickMsg(dialog, text);
            }
        });
        try {
            dialog.show();
        } catch (Exception e) {
        }
    }

    public interface MsgCallBack {
        public void confirmMsg(MessageDialog d);

        public void cancelMsg(MessageDialog d);
    }

    public interface MsgCallBackData {
        public void clickMsg(Dialog d, String data);
    }

    public interface MsgCallBackData2 {
        public void clickMsg(Dialog d, String data, TextView textView);
    }

}
